package com.io.example.demoparent.mq.direct;


import com.io.example.demoparent.mq.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class ReceiveLogsDirect01 {

    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        directProduct01();
    }


    public static  void directProduct01() throws IOException, TimeoutException {
        //获取信道
        Channel rabbitChannel = RabbitMQUtil.getRabbitChannel( );
        //创建交换机
        rabbitChannel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
        //创建队列
        rabbitChannel.queueDeclare("console",Boolean.FALSE,Boolean.FALSE,Boolean.FALSE,null);

        //将队列和交换机判定
        rabbitChannel.queueBind("console",EXCHANGE_NAME,"info");
        rabbitChannel.queueBind("console",EXCHANGE_NAME,"warning");

        rabbitChannel.basicConsume("console",Boolean.TRUE, new DeliverCallback( ) {
            @Override
            public void handle(String s, Delivery delivery) throws IOException {
                System.out.println("接收成功的消息："+ new String( delivery.getBody(), StandardCharsets.UTF_8));
            }
        }, new CancelCallback( ) {
            @Override
            public void handle(String s) throws IOException {

            }
        });
    }

}
